# http://tzxm.jxzwfww.gov.cn/icity/ipro/open/publicity

import requests
import execjs
from openpyxl import Workbook

cookies = {
    'ICITYSession': '5929f3f73f25424aadcbf6d489b9ce50',
}

headers = {
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Content-Type': 'application/json',
    # 'Cookie': 'ICITYSession=5929f3f73f25424aadcbf6d489b9ce50',
    'Origin': 'http://tzxm.jxzwfww.gov.cn',
    'Pragma': 'no-cache',
    'Referer': 'http://tzxm.jxzwfww.gov.cn/icity/ipro/open/publicity',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
    'X-Requested-With': 'XMLHttpRequest',
}
params = execjs.compile(open('解析JS.js','r',encoding='utf-8').read()).call('getParam')

json_data = {
    'page': '3',
    'rows': '10',
    'type': '0',
    'year': '2023',
    'projectName': '',
    'projectCode': '',
}

response = requests.post(
    'http://tzxm.jxzwfww.gov.cn/icity/api-v2/jxtzxm.app.icity.ipro.IproCmd/getDisplayListByPage',
    params=params,
    cookies=cookies,
    headers=headers,
    json=json_data,
    verify=False,
).json()

# 构建工作簿
wb = Workbook()

# 构建工作表
ws = wb.active

# 行标题
row_title = ['ORG_NAME','APPLY_SUBJECT','PROJECT_CODE','ITEM_ID','STATE','PROJECT_NAME','SEQ_ID','ITEM_NAME','ROWNUM_','SUBMIT_TIME','FINISH_TIME','TIME_LIMIT','RECEIVE_NUMBER']
ws.append(row_title)
for i,item in enumerate(response['data']):
    # 根据行标题获取内容
    row_data = [item.get("columns").get(title,"") for title in row_title]

    ws.append(row_data)

wb.save('data.xlsx')